package com.cn.kabalifemerchants.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.ui.login.adapter.SreviceTypeToAdapter;
import com.cn.kabalifemerchants.ui.login.bean.CooperationSreviceListBean;
import com.cn.kabalifemerchants.ui.page.bean.UpImgBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.utils.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/9.
 * function: 服务类型
 */
public class CooperationSreviceActivity extends IBaseActivity<CooperationSreviceView, CooperationSrevicePresenter> implements CooperationSreviceView {


    @BindView(R.id.rv_data)
    RecyclerView rvData;
    Bundle bundle;
    String ids;

    int sign;
    List<CooperationSreviceListBean.ResultBean> mList;
    ListAdapter mAdapter;


    @Override
    protected CooperationSrevicePresenter createPresenter() {
        return new CooperationSrevicePresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_cooperation_srevice;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("商户服务类型");
        bundle = getIntent().getExtras();
        ids = bundle.getString("ids");
        sign = bundle.getInt("sign");
        LoggerUtils.e("有吗：" + ids);
        mPresenter.onCooperationSreviceListData();
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rvData.setLayoutManager(layoutManager);
        rvData.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ListAdapter(R.layout.activity_cooperation_srevice_item, mList, sign);
        //给RecyclerView设置适配器
        rvData.setAdapter(mAdapter);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onCooperationSreviceListSuccess(CooperationSreviceListBean bean) {
        if (!TextUtils.isEmpty(ids)) {
            List<String> result = Arrays.asList(ids.split(","));
            mList = bean.getResult();
            for (int i = 0; i < mList.size(); i++) {
                for (int j = 0; j < mList.get(i).getChildren().size(); j++) {
                    for (int k = 0; k < result.size(); k++) {
                        if (mList.get(i).getChildren().get(j).getId() == Integer.parseInt(result.get(k))) {
                            mList.get(i).setCheck(true);
                            mList.get(i).getChildren().get(j).setCheck(true);
                        }
                    }
                }
            }
            mAdapter.replaceData(mList);
        } else
            mAdapter.replaceData(bean.getResult());
    }

    @Override
    public void onUpImgSuccess(UpImgBean bean) {

    }


    @OnClick(R.id.tv_btn_next)
    public void onViewClicked() {
        List<String> listNmas = new ArrayList<>();
        List<String> listIds = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            for (int j = 0; j < mList.get(i).getChildren().size(); j++) {
                if (mList.get(i).getChildren().get(j).isCheck()) {
                    listIds.add(String.valueOf(mList.get(i).getChildren().get(j).getId()));
                    listNmas.add(String.valueOf(mList.get(i).getChildren().get(j).getTitle()));
                }
            }
        }
        if (listIds.size() > 0) {
            if (listIds.size()>10){
                RxToast.error("服务类型最多选择10项");
            }else {
                Intent intent = new Intent();
                intent.putExtra("names", StringUtil.convertListToString(listNmas));
                intent.putExtra("ids", StringUtil.convertListToString(listIds));
                setResult(1010, intent);
                finish();
            }
        } else {
            RxToast.error("请选择服务类型");
        }
    }


    class ListAdapter extends BaseQuickAdapter<CooperationSreviceListBean.ResultBean, BaseViewHolder> {


        int sign;

        public ListAdapter(int layoutResId, @Nullable List<CooperationSreviceListBean.ResultBean> data, int sign) {
            super(layoutResId, data);
            this.sign = sign;
        }

        @Override
        protected void convert(BaseViewHolder helper, final CooperationSreviceListBean.ResultBean item) {
            helper.setText(R.id.tv_title, item.getTitle());
            RecyclerView rv_data_items = helper.getView(R.id.rv_data_items);
            rv_data_items.setLayoutManager(new GridLayoutManager(getMe(), 3));
            rv_data_items.setNestedScrollingEnabled(false);
            //创建适配器
            SreviceTypeToAdapter typeRigthAdapter = new SreviceTypeToAdapter(R.layout.activity_cooperation_srevice_item_to, item.getChildren(), item.isCheck());
            //给RecyclerView设置适配器
            rv_data_items.setAdapter(typeRigthAdapter);
            typeRigthAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    //todo
                    if (sign == 1) {
                        for (int i = 0; i < mList.size(); i++) {
                            if (item.getId() == mList.get(i).getId())
                                mList.get(i).setCheck(true);
                        }
                        for (int i = 0; i < item.getChildren().size(); i++) {
                            if (i == position) {
                                if (item.getChildren().get(i).isCheck())
                                    item.getChildren().get(i).setCheck(false);
                                else item.getChildren().get(i).setCheck(true);
                            }
                        }
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("names", typeRigthAdapter.getItem(position).getTitle());
                        intent.putExtra("ids", typeRigthAdapter.getItem(position).getId());
                        LoggerUtils.e("选择的id：" + typeRigthAdapter.getItem(position).getId());
                        setResult(1010, intent);
                        finish();
                    }

                    mAdapter.notifyDataSetChanged();
                }
            });
        }

    }
}
